package com.example.demo.service.impl;

import com.example.demo.entity.*;
import com.example.demo.mapper.MarketMapper;
import com.example.demo.service.MarketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MarketServiceImpl implements MarketService {
    @Autowired
    private MarketMapper marketMapper;

    @Override
    public List<MarketTypeInfo> findMarketTypeInfo() {
        return marketMapper.findMarketTypeInfo();
    }

    @Override
    public List<MarketAreaInfo> findMarketAreaInfo() {
        return marketMapper.findMarketAreaInfo();
    }

    @Override
    public List<Market> findMarket() {
        return marketMapper.findMarket();
    }

    @Override
    public List<MarketPointRank> findMarketPointRank() {
        return marketMapper.findMarketPointRank();
    }

    @Override
    public List<MarketPopularityRank> findMarketPopularityRank() {
        return marketMapper.findMarketPopularityRank();
    }

    @Override
    public PageInfo<Market> selectMarketPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Market> markets=marketMapper.findMarket();
        PageInfo<Market> marketPageInfo=new PageInfo<Market>(markets);
        return marketPageInfo;
    }

    @Override
    public Market findMarketByName(String name) {
        return marketMapper.findMarketByName(name);
    }

    @Override
    public void insertMarket(Market market) {
        marketMapper.insertMarket(market);
        marketMapper.addMarketAreaInfo(market.getArea());
        marketMapper.addMarketTypeInfo(market.getType());
        marketMapper.insertMarketPointRank(market);
        marketMapper.insertMarketPopularity(market);
    }

    @Override
    public Market findMarketById(int id) {
        return marketMapper.findMarketById(id);
    }

    @Override
    public void updateMarketEX(Market market1) {
        marketMapper.reduceMarketAreaInfo(market1.getArea());
        marketMapper.reduceMarketTypeInfo(market1.getType());
    }

    @Override
    public void deleteMarket(int id) {
        Market market=marketMapper.findMarketById(id);
        marketMapper.deleteMarketById(id);
        marketMapper.reduceMarketAreaInfo(market.getArea());
        marketMapper.reduceMarketTypeInfo(market.getType());
        marketMapper.deleteMarketPointRankByName(market.getName());
        marketMapper.deleteMarketPopularityRankByName(market.getName());
    }

    @Override
    public void updateMarket(Market market) {
        Market marketdb=marketMapper.findMarketById(market.getId());
        marketMapper.updateMarket(market);
        marketMapper.addMarketAreaInfo(market.getArea());
        marketMapper.addMarketTypeInfo(market.getType());
        MarketPointRank marketPointRank=marketMapper.findMarketPointRankByName(marketdb.getName());
        MarketPopularityRank marketPopularityRank=marketMapper.findMarketPopularityRankByName(marketdb.getName());
        if (marketPointRank!=null){
            market.setId(marketPointRank.getId());
            marketMapper.updateMarketPointRank(market);
        }
        if (marketPopularityRank!=null){
            market.setId(marketPopularityRank.getId());
            marketMapper.updateMarketPopularity(market);
        }

    }
}
